package com.simon.app.october.modules.sys.service.impl;

import com.simon.app.october.common.exception.OctoberException;
import com.simon.app.october.common.utils.PageUtil;
import com.simon.app.october.common.utils.QueryPage;
import com.simon.app.october.modules.sys.dao.SysUserDao;
import com.simon.app.october.modules.sys.entity.SysMenuEntity;
import com.simon.app.october.modules.sys.entity.SysUserEntity;
import com.simon.app.october.modules.sys.service.SysUserService;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class SysUserServiceImpl implements SysUserService {

    @Resource
    private SysUserDao sysUserDao;

    @Override
    public SysUserEntity findByUsername(String username) {
        return sysUserDao.findByUsername(username);
    }

    @Override
    public SysUserEntity findByToken(String token) {
        return sysUserDao.findByToken(token);
    }

    @Override
    public SysUserEntity findById(Long id) {
        return sysUserDao.findById(id);
    }

    @Override
    public List<Long> getAllMenuId(Long userId) {
        return sysUserDao.getAllMenuId(userId);
    }

    @Override
    public PageUtil getPage(QueryPage params) {
        PageUtil<SysUserEntity> pageUtil = new PageUtil<>();

        // query参数
        int page = (int) params.get("page");
        int size = (int) params.get("limit");
        int total = sysUserDao.getTotal(params);
        int pageNum = (int) Math.ceil(total/(double)size);

        pageUtil.setList(sysUserDao.getPage(params));
        pageUtil.setCurrPage(page);
        pageUtil.setTotalCount(total);
        pageUtil.setTotalPage(pageNum);
        pageUtil.setPageSize(size);

        return pageUtil;
    }

    @Override
    public void save(SysUserEntity userEntity) {
        userEntity.setCreateTime(new Date());
        // 随机生成salt
        userEntity.setSalt(RandomStringUtils.randomAlphanumeric(20));

        // 加密密码
        userEntity.setPassword(new Sha256Hash(userEntity.getPassword(), userEntity.getSalt()).toHex());

        // 检查一下是否存在
        checkoutExist(userEntity);

        sysUserDao.insert(userEntity);
    }

    @Override
    public void update(SysUserEntity userEntity) {
        if (StringUtils.isBlank(userEntity.getPassword())) {
            userEntity.setPassword(null);
        } else {
            userEntity.setPassword(new Sha256Hash(userEntity.getPassword(), userEntity.getSalt()).toHex());
        }

        // checkoutExist(userEntity);

        sysUserDao.updateById(userEntity);
    }

    @Override
    public void delete(List<Long> userId) {
        sysUserDao.deleteByIds(userId);
    }

    private void checkoutExist(SysUserEntity userEntity) {
        if (null != sysUserDao.findByUsername(userEntity.getUsername())) {
            throw new OctoberException("该用户名已存在!");
        }
    }
}
